package top.appx.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import top.appx.dao.FoodDao;
import top.appx.entity.FoodEntity;
import top.appx.qo.FoodQO;
import top.appx.util.StringUtil;
import top.appx.vo.Pager;

import java.util.List;

/**
 * Created by john on 2015/7/12.
 */
@Repository
public class FoodDaoImpl extends BaseDaoImpl<FoodEntity> implements FoodDao {

    public Pager<FoodEntity> queryByPage(FoodQO bean, int page, int rows) {
        Criteria c = this.getCurrentSession().createCriteria(FoodEntity.class);
        int total = ((Number)c.setProjection(Projections.rowCount()).uniqueResult()).intValue();
        c.setProjection(null);
       // c.addOrder(Order.desc("id"));
        c.setFirstResult((page-1)*rows);
        c.setMaxResults(rows);
        if(bean!=null) {
            if(!StringUtil.isEmptyOrNull(bean.getRname())){
                c.add(Restrictions.like("rname", "%" + bean.getRname() + "%"));
            }
            if(bean.getPrice()!=null && bean.getPrice()!=0){
                c.add(Restrictions.eq("price", bean.getPrice()));
            }
            if(bean.getStore()!=null && bean.getStore()!=0){
                c.add(Restrictions.eq("store",bean.getStore()));
            }
            if(bean.getCuisine()!=null && bean.getCuisine()!=0){
                c.add(Restrictions.eq("cuisine",bean.getCuisine()));
            }
         /*   if(bean.getStartTime()!=null){
                c.add(Restrictions.lt("createTime", bean.getStartTime()));
            }
            if(bean.getEndTime()!=null){
                c.add(Restrictions.gt("createTime",bean.getEndTime()));
            }*/


        }

        List list = c.list();
        Pager<FoodEntity> result = new Pager<FoodEntity>();
        result.setTotal(total);
        result.setRows(list);
        return result;
    }
}
